package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.unicode.UnicodeServiceOfferKite;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.util.ProductFeeInstUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * @author lpc
 */
@Service
public class StopRentPatternHandler extends AbstractAttrHandler {

    public static final String VALUE = "0";
    public static final String DEFAULT_VALUE_ONE = "1";

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttrVO) {
        String postProvinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String serviceOfferId = querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();
        if (KiteStringUtils.equals(LocalKeyConsts.PROVINCE_SHANGHAI, postProvinceNbr)) {
            // 上海五大双线 停机、预销户/销户时的止租：默认“自动止租”，止租时间以页面录入的止租时间为准，可修改
            commonAttrVO.setValue(VALUE);
            String commonDesc = Optional.ofNullable(AttrValueCache.getAttrValue(commonAttrVO.getAttrId(), commonAttrVO.getValue()))
                .map(AttrValue::getAttrValueName).orElse(commonAttrVO.getValue());
            commonAttrVO.setValueDesc(commonDesc);
            commonAttrVO.setDefaultValue(VALUE);
            setCommonAttr(commonAttrVO);
        }
        // 湖南 停机和销户止租方式默认为“手动止租”，手动止租，自动止租都有
        if (LocalKeyConsts.PROVINCE_HUNAN.equals(postProvinceNbr)) {
            if (KiteStringUtils.equalsAny(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_STOP, UnicodeServiceOfferKite.SEROFF_300013,
                LocalServiceOfferConsts.SERVICE_OFFER_PRE_CANCELLATION, LocalServiceOfferConsts.SERVICE_OFFER_SPECIAL_STOP,LocalServiceOfferConsts.SERVICE_OFFER_XIAOHU)) {
                if (KiteStringUtils.isEmpty(commonAttrVO.getValue())) {
                    commonAttrVO.setValue(DEFAULT_VALUE_ONE); // 手动起租
                    String valueDesc = Optional.ofNullable(AttrValueCache.getAttrValue(commonAttrVO.getAttrId(), commonAttrVO.getValue()))
                        .map(AttrValue::getAttrValueName).orElse(commonAttrVO.getValue());
                    commonAttrVO.setValueDesc(valueDesc);
                    commonAttrVO.setDefaultValue(DEFAULT_VALUE_ONE);
                    setCommonAttr(commonAttrVO);
                }
            }
        }

        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");
        if(KiteStringUtils.isEqual(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_PRE_CANCELLATION) && KiteStringUtils.isEqual(nodeCode, "OrderResubmit")) {
            // 预销户-重提，止租方式不能修改
            KiteAttr stopRentAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.STOP_RENT_WAY);

            commonAttrVO.setIsEdit(KeyConsts.IFTRUE_F);
            stopRentAttr.setIsEdit(KeyConsts.IFTRUE_F);
            operateSceneInstService.setCommonAttr(sceneInstId, stopRentAttr);
            setCommonAttr(commonAttrVO);
        }
    }

    @Override
    public DataResult<Boolean> apply (String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isNotEmpty(value)) {
            if (VALUE.equals(value)) {
                KiteAttr attr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10123");
                // 光纤出租停机没有止租时间
                if (Objects.nonNull(attr)) {
                    attr.setShowType("D");
                    operateSceneCommonService.setCommonAttr(sceneInstId, attr);
                }
            } else {
                KiteAttr attr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10123");// 光纤出租停机没有止租时间
                if (Objects.nonNull(attr)) {
                    attr.setShowType("E");
                    operateSceneCommonService.setCommonAttr(sceneInstId, attr);
                }
            }
        }
        super.apply(sceneInstId, fieldName, value, valueDesc);
        //预销户处理趸交资费的失效时间
//        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
//        if (KiteStringUtils.isEqual(CuccServiceOfferConsts.SERVICE_OFFER_ACCOUNT_CANCEL, sceneInst.getServiceOfferId())) {
//            ProductFeeInstUtils.dealOneOffFeeExpireTime(sceneInstId);
//        }
        return ErrorConsts.SUCCESS.getResult(true);
    }
}
